package com.yjs.app.trade.service.service.business.contract.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yjs.app.trade.core.util.StringUtils;
import com.yjs.app.trade.entity.bean.ContractInfo;
import com.yjs.app.trade.service.mapper.business.contract.ContractMapper;
import com.yjs.app.trade.service.service.business.contract.ContractService;

/**
 * 合同相关业务接口实现
 * 
 * @author Administrator
 *
 */
@Service("contractService")
public class ContractServiceImpl implements ContractService {

	@Autowired
	ContractMapper contractMapper;

	@Override
	public ContractInfo findContractInfoById(String contractId) {
		ContractInfo contractInfo = contractMapper.findContractInfoById(contractId);
		
		if (contractInfo.getMemberAddresses() != null) {
			String address = "";
			for (Map<String, Object> map : contractInfo.getMemberAddresses()) {
				for (Map.Entry<String, Object> entry : map.entrySet()) {
					if (StringUtils.equals("IS_DEFAULE", entry.getKey()) && Integer.parseInt(entry.getValue().toString()) == 1) {
						address = map.get("ADDRESS").toString();
					}
				}
			}
			if (StringUtils.isBlank(address)) {
				address = contractInfo.getMemberAddresses().get(0).get("ADDRESS").toString();
			}
			contractInfo.setAddress(address);
		}
		return contractInfo;
	}

	@Override
	public ContractInfo findContractApparInfoById(String contractId) {
		ContractInfo contractInfo = contractMapper.findContractApparInfoById(contractId);
		if (contractInfo.getMemberAddresses() != null) {
			String address = "";
			for (Map<String, Object> map : contractInfo.getMemberAddresses()) {
				for (Map.Entry<String, Object> entry : map.entrySet()) {
					if (StringUtils.equals("IS_DEFAULE", entry.getKey()) && Integer.parseInt(entry.getValue().toString()) == 1) {
						address = map.get("ADDRESS").toString();
					}
				}
			}
			if (StringUtils.isBlank(address)) {
				address = contractInfo.getMemberAddresses().get(0).get("ADDRESS").toString();
			}
			contractInfo.setAddress(address);
		}
		return contractInfo;
	}

	@Override
	public Long findContractTotalRecordNum(Map<String, String> searchMap) {
		return contractMapper.findContractTotalRecordNum(searchMap);
	}

	@Override
	public List<Map<String,Object>> findContractTypeByContractId(List<String> list){
		return contractMapper.findContractTypeByContractId(list);
	}

}
